#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
验证PyInstallerEx的Python 2.7兼容性
"""

import sys
import os

print("=== PyInstallerEx Python 2.7 Compatibility Verification ===")
print("Python version: {0}".format(sys.version))

# 添加src目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

# 测试导入所有核心模块
modules_to_test = [
    "PyInstallerEx.core.config",
    "PyInstallerEx.core.packager", 
    "PyInstallerEx.utils.file_utils",
    "PyInstallerEx.utils.compression",
    "PyInstallerEx.utils.system_utils",
    "PyInstallerEx.utils.logging"
]

all_passed = True

for module_name in modules_to_test:
    try:
        __import__(module_name)
        print("✅ {0}: Import successful".format(module_name))
    except Exception as e:
        print("❌ {0}: Import failed - {1}".format(module_name, str(e)))
        all_passed = False

# 测试Python 2.7特定的语法特性
print("\n=== Testing Python 2.7 Specific Syntax ===")

# 测试字符串格式化（Python 2风格）
try:
    test_str = "Hello {0} from Python {1}".format("World", sys.version_info[0])
    print("✅ String formatting: {0}".format(test_str))
except Exception as e:
    print("❌ String formatting failed: {0}".format(str(e)))
    all_passed = False

# 测试异常处理（Python 2风格）
try:
    try:
        raise ValueError("Test exception")
    except ValueError as e:
        error_msg = "Caught exception: {0}".format(str(e))
        print("✅ Exception handling: {0}".format(error_msg))
except Exception as e:
    print("❌ Exception handling failed: {0}".format(str(e)))
    all_passed = False

# 测试文件操作（Python 2风格）
try:
    with open("test_temp.txt", "w") as f:
        f.write("Test content")
    
    with open("test_temp.txt", "r") as f:
        content = f.read()
    
    if content == "Test content":
        print("✅ File operations: Working correctly")
    else:
        print("❌ File operations: Content mismatch")
        all_passed = False
    
    # 清理临时文件
    import os
    os.unlink("test_temp.txt")
    
except Exception as e:
    print("❌ File operations failed: {0}".format(str(e)))
    all_passed = False

print("\n=== Verification Results ===")
if all_passed:
    print("🎉 SUCCESS: All compatibility tests passed!")
    print("PyInstallerEx is now compatible with Python 2.7+")
else:
    print("⚠️  WARNING: Some tests failed. Please check the errors above.")

print("\nModified files for Python 2.7 compatibility:")
print("- src/PyInstallerEx/core/config.py")
print("- src/PyInstallerEx/core/packager.py") 
print("- src/PyInstallerEx/utils/file_utils.py")
print("- src/PyInstallerEx/utils/compression.py")
print("- src/PyInstallerEx/utils/system_utils.py")
print("- src/PyInstallerEx/utils/logging.py")
print("- examples/demo.py")
print("- test/test_script.py")
print("- test/test_python27_compatibility.py")
print("- pyproject.toml (already configured)")